﻿//------------------------------------------------------------------------------
// System Name:   Customer Service Platform
// Component:     CIS Overseas  PreferenceManagementHelper
// Created By:    Samara Jiang (samaraj@wicresoft.com)
// Created Date:  2011-2-10
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Text;
using ASBCC.Database.EntityFramework;

namespace ASBCC.Business.CIS
{
    /// <summary>
    /// CIS  海外客户个人喜好 模块数据库访问类
    /// </summary>
    public static class PreferenceManagementHelper
    {
        /// <summary>
        /// 通过ListID查询Levels
        /// </summary>
        public static IEnumerable GetLevelsWithListRelation(int listID)
        {
            using (CCEntities dc = new CCEntities())
            {
                var list = dc.PreferencesLists.FirstOrDefault(p => p.ListID == listID);
                var levels = dc.PreferencesLevels.Where(p => !p.Disabled).ToArray();
                list.PreferencesLevels.Load();
                var source = from p in levels
                             select new
                             {
                                 p.LevelID,
                                 p.Name,
                                 p.Description,
                                 ContainsList = list.PreferencesLevels.Contains(p) ? "checked" : ""
                             };
                return source.ToArray();
            }
        }

        /// <summary>
        /// 通过ListID查询Values
        /// </summary>
        public static IEnumerable GetValuesWithListRelation(int listID)
        {
            using (CCEntities dc = new CCEntities())
            {
                var list = dc.PreferencesLists.FirstOrDefault(p => p.ListID == listID);
                var values = dc.PreferencesValues.Where(p => !p.Disabled).ToArray();
                list.PreferencesValues.Load();
                var source = from p in values
                             select new
                             {
                                 p.ValueID,
                                 p.Name,
                                 p.Description,
                                 ContainsList = list.PreferencesValues.Contains(p) ? "checked" : ""
                             };
                return source.ToArray();
            }
        }

       

        /// <summary>
        /// 为Lists添加Levels和Values
        /// </summary>
        public static void OnListInserting(ObjectContext dc, PreferencesList list, string levelNames,string valueNames)
        {

            if (!String.IsNullOrEmpty(levelNames))
            {
                var levels = dc.CreateQuery<PreferencesLevel>("[PreferencesLevels]").Where(p => p.Disabled == false);
                foreach (var level in levelNames.Split(','))
                {
                    int levelID = int.Parse(level);
                    var levelInList = levels.First(p => p.LevelID == levelID);
                    list.PreferencesLevels.Add(levelInList);
                }

            }
            if (!String.IsNullOrEmpty(valueNames))
            {
                var values = dc.CreateQuery<PreferencesValue>("[PreferencesValues]").Where(p => p.Disabled == false);
                foreach (var value in valueNames.Split(','))
                {
                    int valueID = int.Parse(value);
                    var valueInList = values.First(p => p.ValueID == valueID);
                    list.PreferencesValues.Add(valueInList);
                }

            }

        }

        /// <summary>
        /// 为Lists修改Levels和Values
        /// </summary>
        public static void OnListUpdating(ObjectContext dc, PreferencesList list, string levelNames, string valueNames)
        {
            list.PreferencesLevels.Load();
            list.PreferencesLevels.Clear();
            list.PreferencesValues.Load();
            list.PreferencesValues.Clear();
            OnListInserting(dc, list, levelNames, valueNames);
        }

        /// <summary>
        /// 将一个List的激活状态设为未激活
        /// </summary>
        public static void SetListDisabled(int listID)
        {
            using (var dc = new CCEntities())
            {
                dc.PreferencesLists.FirstOrDefault(p => p.ListID == listID).Disabled = true;
                dc.SaveChanges();
            }
        }

        /// <summary>
        /// 将一个Level的激活状态设为未激活
        /// </summary>
        public static void SetLevelDisabled(int levelID)
        {
            using (var dc = new CCEntities())
            {
                dc.PreferencesLevels.FirstOrDefault(p => p.LevelID == levelID).Disabled = true;
                dc.SaveChanges();
            }
        }

        /// <summary>
        /// 根据当前LevelID获取Name
        /// </summary>
        public static IEnumerable getLevelName(int entityID)
        {
            using (var dc = new CCEntities())
            {
                var name = dc.PreferencesLevels.FirstOrDefault(p => p.LevelID == entityID).Name;
                return name;
            }
        }


        /// <summary>
        /// 根据当前ValueID获取Name
        /// </summary>
        public static IEnumerable getValueName(int entityID)
        {
            using (var dc = new CCEntities())
            {
                var name = dc.PreferencesValues.FirstOrDefault(p => p.ValueID == entityID).Name;
                return name;
            }
        }

        /// <summary>
        /// 根据当前ListID获取Name
        /// </summary>
        public static IEnumerable getListName(int entityID)
        {
            using (var dc = new CCEntities())
            {
                var name = dc.PreferencesLists.FirstOrDefault(p => p.ListID == entityID).Name;
                return name;
            }
        }

        /// <summary>
        /// 将一个Value的激活状态设为未激活
        /// </summary>
        public static void SetValueDisabled(int valueID)
        {
            using (var dc = new CCEntities())
            {
                dc.PreferencesValues.FirstOrDefault(p => p.ValueID == valueID).Disabled = true;
                dc.SaveChanges();
            }
        }

    }
}
